package com.qqtech.qquan.sales.controller;

import java.sql.Timestamp;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.admin.context.AdminPage;
import com.qqtech.core.common.util.StringUtil;
import com.qqtech.core.common.util.TimeUtil;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.mall.enums.MallShopStatusDictEnum;
import com.qqtech.qquan.mall.model.MallShop;
import com.qqtech.qquan.mall.model.MallShopQuery;
import com.qqtech.qquan.mall.model.MallShopVo;
import com.qqtech.qquan.mall.service.MallShopService;
import com.qqtech.qquan.sales.enums.SalesBaseTargetDictEnum;
import com.qqtech.qquan.sales.model.SalesBase;
import com.qqtech.qquan.sales.model.SalesBaseQuery;
import com.qqtech.qquan.sales.model.SalesBaseVo;
import com.qqtech.qquan.sales.service.SalesBaseService;
import com.qqtech.qquan.sys.controller.BaseAdminController;

@Controller
@RequestMapping(value = "/sales/base")
public class SalesBaseController extends BaseAdminController {
	@Resource
	private SalesBaseService salesBaseService;
	@Resource
	private MallShopService mallShopService;
    
	@Override
	protected BaseService<SalesBase> getBaseService() {
		return salesBaseService;
	}
	
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(Model model) {
		return "/sales/salesbase-list";
	}
	
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public AdminPage list(SalesBaseQuery query, HttpServletRequest request,
			HttpServletResponse response) {
		AdminPage adminPage = new AdminPage();
		String rows = (String) request.getParameter("rows"); // 每页条数
		query.setSize(Integer.valueOf(rows));
		Pageable pageKit = new PageKit(query.getPage(), query.getSize(),
				new PageOrder(Direction.DESC, "id"));
		Page<SalesBaseVo> page = salesBaseService.queryPageList(query, pageKit);
		if(page.getContent() != null){
			for(SalesBaseVo salesBase : page.getContent()){
				if(salesBase != null && salesBase.getShopId() != null){
					MallShop shop = mallShopService.getById(salesBase.getShopId());
					salesBase.setShopName(shop.getName());
				}else{
					salesBase.setShopName("无");
				}
			}
		}
		adminPage.setRows(page.getContent());
		adminPage.setTotal(Integer.valueOf(page.getTotalElements() + ""));
		return adminPage;
	}
	
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String add(Model model ,HttpServletRequest request, HttpServletResponse response) {
		MallShopQuery queryShop = new MallShopQuery();
		queryShop.setStatus(MallShopStatusDictEnum.NORMAL.getCode());
		List<MallShopVo> shopList = mallShopService.queryList(queryShop);		
		request.setAttribute("shopList", shopList);
		return "/sales/salesbase-update";
	}
	
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public Result doAdd(SalesBaseVo salesBaseVo, HttpServletRequest request,
			HttpServletResponse response) {
		Result ret = new Result();
		if (salesBaseVo == null) {
			ret.addErr("参数不能为空");
		} else {
			if (salesBaseVo.getTarget() == null) {
				ret.addErr("促销对象不能为空");
			}
			if (salesBaseVo.getTarget() != null && salesBaseVo.getTarget().intValue() == SalesBaseTargetDictEnum.ONESHOP.getCode()
					&& salesBaseVo.getShopId() == null) {
				ret.addErr("未选择店铺");
			}
			if (salesBaseVo.getType() == null) {
				ret.addErr("类型不能为空");
			}
			if (StringUtil.isBlank(salesBaseVo.getName())) {
				ret.addErr("活动名称不为空");
			}
			if (salesBaseVo.getRequirement() == null) {
				ret.addErr("触发条件不能为空");
			}
			if (salesBaseVo.getSubsidy() == null) {
				ret.addErr("补贴额度不能为空");
			}
			if (salesBaseVo.getStatus() == null) {
				ret.addErr("状态不能为空");
			}
			if (salesBaseVo.getSubsidyType() == null) {
				ret.addErr("补贴类型不能为空");
			}
		}
		if (ret.checkForm()) {
			Timestamp now = TimeUtil.now();
			SalesBase salesBase = new SalesBase();
			salesBase.setTarget(salesBaseVo.getTarget());
			salesBase.setType(salesBaseVo.getType());
			salesBase.setName(salesBaseVo.getName().trim());
			salesBase.setStatus(salesBaseVo.getStatus());
			salesBase.setRequirement(salesBaseVo.getRequirement());
			salesBase.setSubsidy(salesBaseVo.getSubsidy());
			salesBase.setShopId(salesBaseVo.getShopId());
			salesBase.setStartAt(salesBaseVo.getStartAt());
			salesBase.setEndAt(salesBaseVo.getEndAt());
			salesBase.setNewAt(now);
			salesBase.setSubsidyType(salesBaseVo.getSubsidyType());
			salesBaseService.insert(salesBase);
		}
		return ret;
	}
	
	@RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
	public String update(@PathVariable Integer id, HttpServletRequest request, HttpServletResponse response) {
		SalesBaseVo salesBaseVo = salesBaseService.getById(id);
		request.setAttribute("salesBaseVo", salesBaseVo);
		
		MallShopQuery queryShop = new MallShopQuery();
		queryShop.setStatus(MallShopStatusDictEnum.NORMAL.getCode());
		List<MallShopVo> shopList = mallShopService.queryList(queryShop);		
		request.setAttribute("shopList", shopList);
	
		return "/sales/salesbase-update";
	}
	
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public Result doUpdate(SalesBaseVo salesBaseVo) {
		Result ret = new Result();
		if (salesBaseVo == null || salesBaseVo.getId() == null) {
			ret.addErr("参数不能为空");
		} else {
			if (salesBaseVo.getTarget() == null) {
				ret.addErr("促销对象不能为空");
			}
			if (salesBaseVo.getType() == null) {
				ret.addErr("类型不能为空");
			}
			if (StringUtil.isBlank(salesBaseVo.getName())) {
				ret.addErr("活动名称不为空");
			}
			if (salesBaseVo.getRequirement() == null) {
				ret.addErr("触发条件不能为空");
			}
			if (salesBaseVo.getSubsidy() == null) {
				ret.addErr("补贴额度不能为空");
			}
			if (salesBaseVo.getStatus() == null) {
				ret.addErr("状态不能为空");
			}
			if (salesBaseVo.getSubsidyType() == null) {
				ret.addErr("补贴类型不能为空");
			}
		}
		if (ret.checkForm()) {
			SalesBase update = new SalesBase();
			update.setTarget(salesBaseVo.getTarget());
			update.setType(salesBaseVo.getType());
			update.setName(salesBaseVo.getName().trim());
			update.setStatus(salesBaseVo.getStatus());
			update.setRequirement(salesBaseVo.getRequirement());
			update.setSubsidy(salesBaseVo.getSubsidy());
			update.setShopId(salesBaseVo.getShopId());
			update.setStartAt(salesBaseVo.getStartAt());
			update.setEndAt(salesBaseVo.getEndAt());
			update.setUpdAt(TimeUtil.now());
			update.setId(salesBaseVo.getId());
			update.setSubsidyType(salesBaseVo.getSubsidyType());
			salesBaseService.updateById(update);
		}
		return ret;
	}
}
